/*
 * Copyright (c) 2025 Huawei Technologies Co., Ltd.
 * openUBMC is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *         http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

#include <mcpy/builtins/global_functions.h>

namespace mcpy::detail {

range_container::size_type range_container::size() const {
    if (m_step == 0) {
        return 0;
    }
    if (m_step > 0) {
        if (m_stop <= m_start) {
            return 0;
        }
        // 对于步长为 1 的情况，直接计算差值
        if (m_step == 1) {
            return static_cast<size_type>(m_stop - m_start);
        }
        // 对于其他步长，使用向上取整
        return static_cast<size_type>((m_stop - m_start + m_step - 1) / m_step);
    } else {
        // 负步长（暂不支持，但保留逻辑）
        if (m_stop >= m_start) {
            return 0;
        }
        return static_cast<size_type>((m_start - m_stop - m_step - 1) / (-m_step));
    }
}

} // namespace mcpy::detail
